package com.study.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

public class JwtMain {
    public static void main(String[] args) {
        //jwt携带信息
        long userId=1233333333;
        String password="dfds123456";
        String token = JWT.create().withAudience(userId+"","tanhaiyuan","发放范德萨防守打法","徐晓细小范德萨").withExpiresAt(new Date(System.currentTimeMillis()+1000))
                .sign(Algorithm.HMAC256(password));

        System.out.println(token);

        //jwt 刷新  [JWT\(JSON Web Token\)自动延长到期时间 \- 纯净的天空](https://vimsky.com/article/3601.html)
        //[flask \- json web token过期后怎么搞 \- SegmentFault 思否](https://segmentfault.com/q/1010000002449556)

        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(password)).build();
        try {
            DecodedJWT decodedJWT = jwtVerifier.verify(token);
            System.out.println(decodedJWT.toString());
        } catch (JWTVerificationException e) {
            throw new RuntimeException("401");
        }

        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        jwtVerifier = JWT.require(Algorithm.HMAC256(password)).build();
        try {
            DecodedJWT decodedJWT = jwtVerifier.verify(token);
            System.out.println(decodedJWT.toString());
        } catch (JWTVerificationException e) {
            e.printStackTrace();
        }

        //accesstoken , refreshtoken
    }
}
